﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nina.Codec
{
    /// <summary>
    /// A codec for suffixing packets with a delimiter
    /// </summary>
    public class DelimiterCodec : NinaCodec
    {
        private byte[] m_delimiterBytes;
        private char m_delimiter;
        private Encoding m_encoding;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="delimiter">Delimiter which should be used</param>
        /// <param name="enc">Delimiter encoding</param>
        public DelimiterCodec(char delimiter, Encoding enc)
        {
            m_delimiter = delimiter;
            m_encoding = enc;
            m_delimiterBytes = enc.GetBytes(Convert.ToString(m_delimiter));
        }

        public override void OnSessionStarted(NinaSession session)
        {
            /* Do nothing */
        }

        public override void OnSessionClosed(NinaSession session)
        {
            /* Do nothing */
        }

        public override bool OnRead(NinaSession session, ref byte[] data, ref byte[] remaining)
        {
            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] == m_delimiterBytes[0])
                {
                    /* Determine if a delimiter is present */
                    bool delPresent = true;
                    for (int j = i; j - i < m_delimiterBytes.Length && j < data.Length; j++)
                    {
                        if (data[j] != m_delimiterBytes[j - i])
                        {
                            delPresent = false;
                            break;
                        }
                    }

                    if (delPresent)
                    {
                        /* 
                         * Delimiter was found, update objects
                         * 'data' = all data before delimiter
                         * place any other data after delimiter into 'remaining'
                         */
                        List<byte> newData = new List<byte>();
                        for (int j = 0; j < i; j++)
                        {
                            newData.Add(data[j]);
                        }


                        List<byte> remainingData = new List<byte>();
                        for (int j = i + m_delimiterBytes.Length; j < data.Length; j++)
                        {
                            remainingData.Add(data[j]);
                        }

                        data = newData.ToArray<byte>();
                        remaining = remainingData.ToArray<byte>();

                        /* A full packet was read, return true */
                        return true;
                    }
                }
            }

            /* Not a full packet */
            remaining = data;
            return false;
        }

        public override byte[] OnWrite(NinaSession session, byte[] data)
        {
            /* Append delimiter */
            List<byte> result = new List<byte>();
            result.AddRange(data);
            result.AddRange(m_delimiterBytes);
            return result.ToArray<byte>();
        }
    }
}
